package com.csit.util;

import java.util.LinkedList;
import java.util.List;

public class StringUtil {

	public static boolean isNull(final String str) {
		if (str == null || str.trim().length() == 0)
			return true;
		return false;
	}

	public static boolean isNotNull(final String str){
		return !isNull(str);
	}
	
	public static String[] split(String data, char token) {
		if (data == null)
			return null;
		// int tokens = 0;

		LinkedList<Integer> tokens = new LinkedList<Integer>();
		for (int i = 0; i < data.length(); i++) {
			char c = data.charAt(i);
			if (c == '\"') {
				while (++i < data.length()) {
					if (data.charAt(i) == '\"')
						break;
				}
			} else if (data.charAt(i) == token)
				tokens.add(i);
		}

		String[] r = new String[tokens.size() + 1];

		int fromIdx = 0;
		for (int i = 0; i < tokens.size(); i++) {
			int idx = tokens.get(i);
			r[i] = data.substring(fromIdx, idx);
			fromIdx = idx + 1;
		}

		r[r.length - 1] = data.substring(fromIdx, data.length());

		return r;
	}

	public static boolean in(String str, String[] strs) {
		if (str == null || strs == null)
			return false;
		for (int i = 0; i < strs.length; i++) {
			if (str.equals(strs[i]))
				return true;
		}
		return false;
	}

	public static boolean in(String str, List<String> strs) {
		if (str == null || strs == null)
			return false;
		for (int i = 0; i < strs.size(); i++) {
			if (str.equals(strs.get(i)))
				return true;
		}
		return false;
	}

	/**
	 * 按照十六进制打印byte
	 * 
	 * @param b
	 * @return
	 */
	public static synchronized String toPrintable(byte[] b) {
		final char[] alphabet = ("abcdefghijklmnopqrstuvwxyz"
				+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "1234567890").toCharArray();
		char[] out = new char[b.length];
		for (int i = 0; i < b.length; i++) {
			int index = b[i] % alphabet.length;
			if (index < 0)
				index += alphabet.length;
			out[i] = alphabet[index];
		}
		return new String(out);
	}
}
